﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MuzičkiFeršterker
{
    /// <summary>
    /// Class used to handle media types handled with Windows Media Element/Player
    /// </summary>
    class Play_MediaElement : Play_
    {
        private bool finished = false;

        /// <summary>
        /// Constructor that loads a new song and engine for playing
        /// </summary>
        /// <param name="inputSong">Input song</param>
        public Play_MediaElement(Song inputSong)
            : base(inputSong)
        {
            mediaElementEngine.Source = new Uri(currentSong.Path);
            mediaElementEngine.MediaEnded += mediaElementEngine_MediaEnded;
        }

        /// <summary>
        /// Method that activates when current song ends
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event arguments</param>
        private void mediaElementEngine_MediaEnded(object sender, System.Windows.RoutedEventArgs e)
        {
            finished = true;
        }

        /// <summary>
        /// Starts playback of a MediaElement engine song
        /// </summary>
        public override void Play()
        {
            mediaElementEngine.Play();
            finished = false;
        }

        /// <summary>
        /// DEPRECATED - throws exception, here because of IPlay implementation
        /// </summary>
        [Obsolete("PlayPause now handled by engine handler")]
        public override void PlayPause()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Pauses the current song
        /// </summary>
        public override void Pause()
        {
            mediaElementEngine.Pause();
            finished = false;
        }

        /// <summary>
        /// Resumes the current song
        /// </summary>
        public override void Resume()
        {
            mediaElementEngine.Play();
            finished = false;
        }

        /// <summary>
        /// Stopps the current song and sets the time to 0
        /// </summary>
        public override void Stop()
        {
            mediaElementEngine.Stop();
        }

        /// <summary>
        /// Returns position of current song
        /// </summary>
        /// <returns>Time in ms</returns>
        public override int getPosition()
        {
            int a = 0;
            mediaElementEngine.Dispatcher.Invoke((Action)delegate
            {
                a = (int)mediaElementEngine.Position.TotalMilliseconds;
            });
            return a;
        }

        /// <summary>
        /// Sets the playback position
        /// </summary>
        /// <param name="pos">Time in ms</param>
        public override void setPosition(int pos)
        {
            mediaElementEngine.Position = TimeSpan.FromMilliseconds(pos);
        }

        /// <summary>
        /// Method that returns status of playing song
        /// </summary>
        /// <returns>TRUE if song is finished, FALSE if it's not</returns>
        public override bool isFinished()
        {
            return finished;
        }
    }
}
